package cn.maszy.service.impl.projectmanagement;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.maszy.common.DateUtils;
import cn.maszy.common.Pager;
import cn.maszy.common.SessionFactory;
import cn.maszy.dao.projectmanagement.B_T_Flow_ProjectManagementMapper;
import cn.maszy.dao.projectmanagement.B_T_Ufp_StatusMapper;
import cn.maszy.dao.projectmanagement.B_T_Ufp_TaskHandoutMapper;
import cn.maszy.dao.projectmanagement.B_T_Ufp_TaskManagerMapper;
import cn.maszy.dao.systemsetting.B_T_Sys_OperatingLogsMapper;
import cn.maszy.model.projectmanagement.B_T_Flow_ProjectManagement;
import cn.maszy.model.projectmanagement.B_T_Flow_ProjectManagementExample;
import cn.maszy.model.projectmanagement.B_T_Ufp_Status;
import cn.maszy.model.projectmanagement.B_T_Ufp_StatusExample;
import cn.maszy.model.projectmanagement.B_T_Ufp_TaskHandout;
import cn.maszy.model.projectmanagement.B_T_Ufp_TaskHandoutExample;
import cn.maszy.model.projectmanagement.B_T_Ufp_TaskManager;
import cn.maszy.model.projectmanagement.B_T_Flow_ProjectManagementExample.Criteria;
import cn.maszy.model.projectmanagement.B_T_Ufp_TaskManagerExample;
import cn.maszy.model.systemsetting.B_T_Sys_OperatingLogs;
import cn.maszy.service.projectmanagement.B_T_Flow_ProjectManagementService;

/** 
 * @浣滆� Allen Shu 
 * @鍒涘缓鏃ユ湡 2012-6-2 
 * @鐗堟湰 V 1.0 
 */
@Service
public class B_T_Flow_ProjectManagementServiceImpl implements B_T_Flow_ProjectManagementService{
	@Autowired
	private B_T_Flow_ProjectManagementMapper b_T_Flow_ProjectManagementMapper;
	@Autowired
	private B_T_Ufp_StatusMapper b_T_Ufp_StatusMapper;
	@Autowired
	private B_T_Ufp_TaskHandoutMapper b_T_Ufp_TaskHandoutMapper;
	@Autowired
	private B_T_Ufp_TaskManagerMapper b_T_Ufp_TaskManagerMapper;
	@Autowired
	private B_T_Sys_OperatingLogsMapper b_T_Sys_OperatingLogsMapper;
	/* (non-Javadoc)
	 * @see cn.maszy.service.investment.B_T_Ufp_AdminService#update(cn.maszy.model.investment.B_T_Ufp_Admin, cn.maszy.model.investment.B_T_Ufp_AdminExample)
	 */
	public int update(B_T_Flow_ProjectManagement b_T_Flow_ProjectManagement,String flag) {
		// TODO Auto-generated method stub
		B_T_Ufp_TaskHandout b_T_Ufp_TaskHandout = new B_T_Ufp_TaskHandout();
		B_T_Sys_OperatingLogs b_T_Sys_OperatingLogs= new B_T_Sys_OperatingLogs();
		 if("add".equals(flag)){
			//b_T_Flow_ProjectManagement.setProjectUuid(UUID.randomUUID().toString());
			b_T_Flow_ProjectManagement.setStatus("0");
			b_T_Flow_ProjectManagement.setIsDelete(0);
			b_T_Flow_ProjectManagement.setCreateTime(new Date());
			b_T_Flow_ProjectManagement.setCreateUser(SessionFactory.getSessionUserRealNam());
			int i =  b_T_Flow_ProjectManagementMapper.insert(b_T_Flow_ProjectManagement);
			//任务跟踪  type_status 1.鏂板  2.鍒犻櫎  3.淇敼  4.瀵煎嚭 
			b_T_Ufp_TaskHandout.setTaskhandId(UUID.randomUUID().toString());
			b_T_Ufp_TaskHandout.setProjectId(b_T_Flow_ProjectManagement.getProjectUuid());
			b_T_Ufp_TaskHandout.setTaskName(b_T_Flow_ProjectManagement.getProjectName());
			b_T_Ufp_TaskHandout.setHandzettelContent(SessionFactory.getSessionUserRealNam()+"创建了["+b_T_Flow_ProjectManagement.getProjectName()+"]项目,并交给了"+b_T_Flow_ProjectManagement.getStaffName()+"负责");
			b_T_Ufp_TaskHandout.setTypeStatus("1");
			b_T_Ufp_TaskHandout.setRemark("创建项目");
			b_T_Ufp_TaskHandout.setUserId(SessionFactory.getSessionUserId());
			b_T_Ufp_TaskHandout.setUserName(SessionFactory.getSessionUserRealNam());
			b_T_Ufp_TaskHandout.setIsDelete(0);
			b_T_Ufp_TaskHandout.setAcceptedTime(DateUtils.nows());
			b_T_Ufp_TaskHandout.setCreateTime(new Date());
			b_T_Ufp_TaskHandout.setCreateUser(SessionFactory.getSessionUserRealNam());
			b_T_Ufp_TaskHandout.setUpdateUser(SessionFactory.getSessionUserRealNam());
			b_T_Ufp_TaskHandout.setUpdateTime(new Date());
			b_T_Ufp_TaskHandoutMapper.insert(b_T_Ufp_TaskHandout);
			/**
			 * 操作记录和工作日志  type:1.正常操作记录 2.工作日志
			 */
			b_T_Sys_OperatingLogs.setLogsId(UUID.randomUUID().toString());
			b_T_Sys_OperatingLogs.setOperatingName(SessionFactory.getSessionUserRealNam());
			b_T_Sys_OperatingLogs.setTaskId(b_T_Flow_ProjectManagement.getProjectUuid());
			b_T_Sys_OperatingLogs.setModuleId("");
			b_T_Sys_OperatingLogs.setModuleName("项目管理");
			b_T_Sys_OperatingLogs.setOperatingContent(SessionFactory.getSessionUserRealNam()+"创建了["+b_T_Flow_ProjectManagement.getProjectName()+"]项目,并交给了"+b_T_Flow_ProjectManagement.getStaffName()+"负责");
			b_T_Sys_OperatingLogs.setOperatingTime(DateUtils.nows());
			b_T_Sys_OperatingLogs.setType(2);
			b_T_Sys_OperatingLogs.setRemark("创建项目");
			b_T_Sys_OperatingLogs.setIsDelete(0);
			b_T_Sys_OperatingLogs.setCreateUser(SessionFactory.getSessionUserRealNam());
			b_T_Sys_OperatingLogs.setCreateTime(new Date());
			b_T_Sys_OperatingLogs.setUpdateUser(SessionFactory.getSessionUserRealNam());
			b_T_Sys_OperatingLogs.setUpdateTime(new Date());
			b_T_Sys_OperatingLogsMapper.insert(b_T_Sys_OperatingLogs);
			 return i;
		 }
		 else
		 {
				b_T_Flow_ProjectManagement.setUpdateTime(new Date());
				b_T_Flow_ProjectManagement.setUpdateUser(SessionFactory.getSessionUserRealNam());
			 int i = b_T_Flow_ProjectManagementMapper.updateByPrimaryKeySelective(b_T_Flow_ProjectManagement);
			//任务跟踪 type_status 1.鏂板  2.鍒犻櫎  3.淇敼  4.瀵煎嚭 
				b_T_Ufp_TaskHandout.setTaskhandId(UUID.randomUUID().toString());
				b_T_Ufp_TaskHandout.setProjectId(b_T_Flow_ProjectManagement.getProjectUuid());
				b_T_Ufp_TaskHandout.setTaskName(b_T_Flow_ProjectManagement.getProjectName());
				b_T_Ufp_TaskHandout.setHandzettelContent(SessionFactory.getSessionUserRealNam()+"更新了["+b_T_Flow_ProjectManagement.getProjectName()+"]项目,并交给了"+b_T_Flow_ProjectManagement.getStaffName()+"负责");
				b_T_Ufp_TaskHandout.setTypeStatus("3");
				b_T_Ufp_TaskHandout.setRemark("更新项目");
				b_T_Ufp_TaskHandout.setUserId(SessionFactory.getSessionUserId());
				b_T_Ufp_TaskHandout.setUserName(SessionFactory.getSessionUserRealNam());
				b_T_Ufp_TaskHandout.setUpdateTime(new Date());
				b_T_Ufp_TaskHandout.setAcceptedTime(DateUtils.nows());
				b_T_Ufp_TaskHandout.setUpdateUser(SessionFactory.getSessionUserRealNam());
				b_T_Ufp_TaskHandout.setCreateUser(SessionFactory.getSessionUserRealNam());
				b_T_Ufp_TaskHandout.setCreateTime(new Date());
				b_T_Ufp_TaskHandoutMapper.insert(b_T_Ufp_TaskHandout);
				/**
				 * 操作记录和工作日志  type:1.正常操作记录 2.工作日志
				 */
				b_T_Sys_OperatingLogs.setLogsId(UUID.randomUUID().toString());
				b_T_Sys_OperatingLogs.setOperatingName(SessionFactory.getSessionUserRealNam());
				b_T_Sys_OperatingLogs.setTaskId(b_T_Flow_ProjectManagement.getProjectUuid());
				b_T_Sys_OperatingLogs.setModuleId("");
				b_T_Sys_OperatingLogs.setModuleName("项目管理");
				b_T_Sys_OperatingLogs.setOperatingContent(SessionFactory.getSessionUserRealNam()+"更新了["+b_T_Flow_ProjectManagement.getProjectName()+"]项目,并交给了"+b_T_Flow_ProjectManagement.getStaffName()+"负责");
				b_T_Sys_OperatingLogs.setOperatingTime(DateUtils.nows());
				b_T_Sys_OperatingLogs.setType(2);
				b_T_Sys_OperatingLogs.setRemark("更新项目");
				b_T_Sys_OperatingLogs.setIsDelete(0);
				b_T_Sys_OperatingLogs.setCreateUser(SessionFactory.getSessionUserRealNam());
				b_T_Sys_OperatingLogs.setCreateTime(new Date());
				b_T_Sys_OperatingLogs.setUpdateUser(SessionFactory.getSessionUserRealNam());
				b_T_Sys_OperatingLogs.setUpdateTime(new Date());
				b_T_Sys_OperatingLogsMapper.insert(b_T_Sys_OperatingLogs);
				//任务
				B_T_Ufp_TaskManager b_T_Ufp_TaskManager = new B_T_Ufp_TaskManager();
				B_T_Ufp_TaskManagerExample example = new B_T_Ufp_TaskManagerExample();
				b_T_Ufp_TaskManager.setRespectiveProject(b_T_Flow_ProjectManagement.getProjectName());
				example.or().andProjectUuidEqualTo(b_T_Flow_ProjectManagement.getProjectUuid());
				b_T_Ufp_TaskManagerMapper.updateByExampleSelective(b_T_Ufp_TaskManager, example);
			 return i;
		 }
	}


	public List<B_T_Ufp_TaskHandout> queryTask(String uuid) {
		// TODO Auto-generated method stub
		B_T_Ufp_TaskHandoutExample example = new B_T_Ufp_TaskHandoutExample();
		example.or().andProjectIdEqualTo(uuid);
		List<B_T_Ufp_TaskHandout> list_B_T_Ufp_TaskHandout = b_T_Ufp_TaskHandoutMapper.selectByExample(example);
		return list_B_T_Ufp_TaskHandout;
	}


	/* (non-Javadoc)
	 * @see cn.maszy.service.investment.B_T_Ufp_AdminService#selectOne(java.lang.String)
	 */
	public B_T_Flow_ProjectManagement selectOne(String staffId) {
		// TODO Auto-generated method stub
		B_T_Flow_ProjectManagement record=b_T_Flow_ProjectManagementMapper.selectByPrimaryKey(staffId);
		if(record == null){
			record = new B_T_Flow_ProjectManagement();
		}
		return record;
	}

	/* (non-Javadoc)
	 * @see cn.maszy.service.investment.B_T_Ufp_AdminService#delete(java.lang.String)
	 */
	public int delete(String checkIds) {
		// TODO Auto-generated method stub
		
		for (String id : checkIds.split(", ")) {
			
			b_T_Flow_ProjectManagementMapper.deleteByPrimaryKey(id);
		}
		return 1;
	}
	/* (non-Javadoc)
	 * @see cn.maszy.service.investment.B_T_Ufp_AdminService#delete(java.lang.String)
	 */
	public int updatekey(String checkIds) {
		// TODO Auto-generated method stub
		B_T_Flow_ProjectManagement bt= new B_T_Flow_ProjectManagement();
		B_T_Ufp_TaskHandout b_T_Ufp_TaskHandout = new B_T_Ufp_TaskHandout();
		B_T_Sys_OperatingLogs b_T_Sys_OperatingLogs= new B_T_Sys_OperatingLogs();
		B_T_Flow_ProjectManagementExample example = new B_T_Flow_ProjectManagementExample();
		for (String id : checkIds.split(", ")) {
			if(!id.equals("on")){
				example.or().andProjectUuidEqualTo(id);
				bt = b_T_Flow_ProjectManagementMapper.selectByPrimaryKey(id);
				bt.setIsDelete(1);
				b_T_Flow_ProjectManagementMapper.updateByPrimaryKeySelective(bt);
				//鎻掑叆璁板綍琛�
				b_T_Ufp_TaskHandout.setTaskhandId(UUID.randomUUID().toString());
				b_T_Ufp_TaskHandout.setProjectId(bt.getProjectUuid());
				b_T_Ufp_TaskHandout.setTaskName(bt.getProjectName());
				b_T_Ufp_TaskHandout.setHandzettelContent(SessionFactory.getSessionUserRealNam()+"删除了["+bt.getProjectName()+"]项目");
				b_T_Ufp_TaskHandout.setTypeStatus("2");
				b_T_Ufp_TaskHandout.setRemark("删除项目");
				b_T_Ufp_TaskHandout.setUserId(SessionFactory.getSessionUserId());
				b_T_Ufp_TaskHandout.setUserName(SessionFactory.getSessionUserRealNam());
				b_T_Ufp_TaskHandout.setIsDelete(0);
				b_T_Ufp_TaskHandout.setCreateTime(new Date());
				b_T_Ufp_TaskHandout.setCreateUser(SessionFactory.getSessionUserRealNam());
				b_T_Ufp_TaskHandout.setUpdateTime(new Date());
				b_T_Ufp_TaskHandout.setUpdateUser(SessionFactory.getSessionUserRealNam());
				b_T_Ufp_TaskHandoutMapper.insert(b_T_Ufp_TaskHandout);
				/**
				 * 操作记录和工作日志  type:1.正常操作记录 2.工作日志
				 */
				b_T_Sys_OperatingLogs.setLogsId(UUID.randomUUID().toString());
				b_T_Sys_OperatingLogs.setOperatingName(SessionFactory.getSessionUserRealNam());
				b_T_Sys_OperatingLogs.setTaskId(bt.getProjectUuid());
				b_T_Sys_OperatingLogs.setModuleId("");
				b_T_Sys_OperatingLogs.setModuleName("项目管理");
				b_T_Sys_OperatingLogs.setOperatingContent(SessionFactory.getSessionUserRealNam()+"删除了["+bt.getProjectName()+"]项目");
				b_T_Sys_OperatingLogs.setOperatingTime(DateUtils.nows());
				b_T_Sys_OperatingLogs.setType(2);
				b_T_Sys_OperatingLogs.setRemark("删除项目");
				b_T_Sys_OperatingLogs.setIsDelete(0);
				b_T_Sys_OperatingLogs.setCreateUser(SessionFactory.getSessionUserRealNam());
				b_T_Sys_OperatingLogs.setCreateTime(new Date());
				b_T_Sys_OperatingLogs.setUpdateUser(SessionFactory.getSessionUserRealNam());
				b_T_Sys_OperatingLogs.setUpdateTime(new Date());
				b_T_Sys_OperatingLogsMapper.insert(b_T_Sys_OperatingLogs);
			}
		}
		return 1;
	}

	public int statusUpdate(String checkIds) {
		// TODO Auto-generated method stub
		B_T_Flow_ProjectManagement bt= new B_T_Flow_ProjectManagement();
		B_T_Flow_ProjectManagementExample example = new B_T_Flow_ProjectManagementExample();
		for (String id : checkIds.split(", ")) {
			if(!id.equals("on")){
				example.or().andProjectUuidEqualTo(id);
				bt = b_T_Flow_ProjectManagementMapper.selectByPrimaryKey(id);
				bt.setStatus("1");
				SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//璁剧疆鏃ユ湡鏍煎紡
				bt.setCloseTime(df.format(new Date()));// new Date()鏃堕棿涓鸿幏鍙栧綋鍓嶇郴缁�
				b_T_Flow_ProjectManagementMapper.updateByPrimaryKeySelective(bt);
			}
		}
		return 1;
	}


	/* (non-Javadoc)
	 * @see cn.maszy.service.investment.B_T_Ufp_AdminService#count(cn.maszy.model.investment.B_T_Ufp_AdminExample)
	 */
	public int count(B_T_Flow_ProjectManagementExample example,B_T_Flow_ProjectManagementMapper b_T_Flow_ProjectManagementMapper) {
		// TODO Auto-generated method stub
		return b_T_Flow_ProjectManagementMapper.countByExample(example);
	}

	/* (non-Javadoc)
	 * @see cn.maszy.service.investment.B_T_Ufp_AdminService#selectList(cn.maszy.model.investment.B_T_Ufp_Admin)
	 */
	public Pager<B_T_Flow_ProjectManagement> selectList(B_T_Flow_ProjectManagement b_T_Flow_ProjectManagement,Pager<B_T_Flow_ProjectManagement> pager,String flag) {
		// TODO Auto-generated method stub
		B_T_Flow_ProjectManagementExample example=new B_T_Flow_ProjectManagementExample();
		Criteria criteria = example.createCriteria();
			if(b_T_Flow_ProjectManagement!=null){
			if(b_T_Flow_ProjectManagement.getProjectName()!=null && !"".equals(b_T_Flow_ProjectManagement.getProjectName())){
				criteria.andProjectNameLike("%"+b_T_Flow_ProjectManagement.getProjectName()+"%");
			} 
			if(b_T_Flow_ProjectManagement.getStaffName() != null && !"".equals(b_T_Flow_ProjectManagement.getStaffName()))
			 {
				criteria.andStaffNameLike("%"+b_T_Flow_ProjectManagement.getStaffName()+"%");
			 }
			if(b_T_Flow_ProjectManagement.getStatus() != null && !"".equals(b_T_Flow_ProjectManagement.getStatus()))
			 {
				criteria.andStatusEqualTo(b_T_Flow_ProjectManagement.getStatus());
			 }
		}
		criteria.andIsDeleteEqualTo(0);
		example.or(criteria);
		int totalCount=new B_T_Flow_ProjectManagementServiceImpl().count(example,b_T_Flow_ProjectManagementMapper);
		pager.setTotalRecords(totalCount);
		example.setLimitStart(pager.getStartRecord());
		example.setLimitEnd(pager.getPageSize());
		example.setOrderByClause(pager.getOrderField()+" "+pager.getOrderDirection());
		
		// 生成list
		List<B_T_Flow_ProjectManagement>  list=b_T_Flow_ProjectManagementMapper.selectByExample(example);
		if(!"1".equals(flag)){
			for(int i=0; i<list.size();i++){
				//查询总任务数
				B_T_Ufp_TaskManagerExample example1 = new B_T_Ufp_TaskManagerExample();
				cn.maszy.model.projectmanagement.B_T_Ufp_TaskManagerExample.Criteria criteria1 = example1.createCriteria();
				criteria1.andProjectUuidEqualTo(list.get(i).getProjectUuid());
				criteria1.andIsDeleteEqualTo(0);
				example1.or(criteria1);
				int j =b_T_Ufp_TaskManagerMapper.countByExample(example1);
				example1.clear();
				//查询已完成的任务数
				criteria1.andProjectUuidEqualTo(list.get(i).getProjectUuid());
				criteria1.andTaskSpeedEqualTo("100%");
				criteria1.andIsDeleteEqualTo(0);
				example1.or(criteria1);
				int k =b_T_Ufp_TaskManagerMapper.countByExample(example1);
				list.get(i).setProjectName(list.get(i).getProjectName()+"<font color='red'>&lt;"+Integer.toString(k)+"/"+Integer.toString(j)+"&gt;</font>");
			}
		}
		pager.setPageRecords(list);
		return pager;
	}

	/**
	 * 鏌ヨ鐘舵�琛�
	 */
	public List<B_T_Ufp_Status> queryStatus() {
		// TODO Auto-generated method stub
		B_T_Ufp_StatusExample example = new B_T_Ufp_StatusExample();
		List<B_T_Ufp_Status> list_B_T_Ufp_Status = b_T_Ufp_StatusMapper.selectByExample(example);
		return list_B_T_Ufp_Status;
	}
	
	
}

